﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRM.DataLayer;

namespace CRM.Business
{
    public class Messages
    {
        /// <summary>
        /// 访客留言添加,留言编号自动添加
        /// </summary>
        /// <param name="visitorName">留言人</param>
        /// <param name="visitorContact">留言人联系方式</param>
        /// <param name="visitorMessage">留言内容</param>
        /// <returns>成功添加返回true，否则返回false</returns>
        public static bool AddVisitorMessage(string visitorName, string visitorContact, string visitorMessage)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    CRM.DataLayer.MessageInfor message = new DataLayer.MessageInfor();                   
                    message.name = visitorName;
                    message.contact = visitorContact;
                    message.visitTime = DateTime.Now;
                    message.messageText = visitorMessage;
                    message.isPublish = false;
                    if (message != null)
                    {
                        db.AddToMessageInfor(message);
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 返回通过审核的、分页的访客留言信息
        /// </summary>
        /// <returns></returns>
        public static IList<DataLayer.MessageInfor> PagePublishedListVisitorMessage(out int totleCount, int pageNum, int pageSize)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = from p in db.MessageInfor
                                 where p.isPublish == true
                                 orderby p.visitTime descending
                                 select p;

                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        /// <summary>
        /// 返回未通过审核的分页的访客留言信息
        /// </summary>
        /// <returns></returns>
        public static IList<DataLayer.MessageInfor> PageListVisitorMessage(out int totleCount, int pageNum, int pageSize)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var query = from p in db.MessageInfor
                                orderby p.visitTime descending
                                select p;
                    List<CRM.DataLayer.MessageInfor> result = new List<MessageInfor>();
                    foreach (var item in query)
                    {
                        item.messageText =ContentInfo.CutString(item.messageText, 46);
                        result.Add(item);
                    }

                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        /// <summary>
        /// 获得通过审核的留言数
        /// </summary>
        /// <returns></returns>
        public static int GetPublishedCount()
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var reslut = from p in db.MessageInfor
                                 where p.isPublish == true
                                 select p;
                    if (reslut != null)
                    {
                        return reslut.Count();
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 根据id号获取一条留言信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataLayer.MessageInfor GetMessageById(int id)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var reslut = db.MessageInfor.SingleOrDefault(p => p.ID == id);
                    if (reslut != null)
                    {
                        return reslut;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        /// <summary>
        /// 删除访客留言
        /// </summary>
        /// <param name="message">留言实体参数</param>
        /// <returns>如果操作成功返回true;反之，返回false</returns>
        public static bool DeleteVisitorMessage(DataLayer.MessageInfor message)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var result = db.MessageInfor.SingleOrDefault(p => p.ID == message.ID);

                    if (result != null)
                    {
                        db.DeleteObject(result);
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 发布访客留言
        /// </summary>
        /// <param name="message">留言实体参数</param>
        /// <returns>如果操作成功返回true;反之，返回false</returns>
        public static bool PostVisitorMessage(DataLayer.MessageInfor message)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = db.MessageInfor.SingleOrDefault(p => p.ID == message.ID);

                    if (result != null)
                    {
                        result.isPublish = message.isPublish;
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 回复留言
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool ReplyVisitorMessage(DataLayer.MessageInfor message)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = db.MessageInfor.SingleOrDefault(p => p.ID == message.ID);

                    if (result != null)
                    {
                        result.reply = message.reply;
                        result.replyTime = message.replyTime;
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
